//
//  OtherImageReportArrayViewController.m
//  yilutongxing
//
//  Created by T&J on 2017/12/7.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "OtherImageReportArrayViewController.h"
#import "ImageReportArrayCell.h"
#import "RPCheckNote.h"

#define IMAGEREPORT_ARRAY_CELL @"rp_array_cell"

@interface OtherImageReportArrayViewController ()<ImageReportArrayDelegate>

@end

@implementation OtherImageReportArrayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"其它影像学报告";
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(save)];
    UIImage *leftImage=[[UIImage imageNamed:@"返回"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *leftButtonItem=[[UIBarButtonItem alloc]initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem=leftButtonItem;
    
    [self initUI];
    [self initData];
}

#pragma mark - UI事件
- (void)save{
    NSEnumerator *revers = [self.report_arr reverseObjectEnumerator];
    RPImageChecknote *note = nil;
    while (note = [revers nextObject]) {
        if (note.images.count==0) {
            [self.report_arr removeObject:note];
        }
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(backImageArray:)]) {
        [self.delegate backImageArray:self.report_arr];
    }
    
    [self back];
}
- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 业务逻辑
- (void)initUI{
    UIView *bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, self.tableView.frame.size.height)];
    [bgview setBackgroundColor:COLOR(248, 248, 248)];
    [self.tableView setBackgroundView:bgview];
    [self.tableView setAllowsSelection:NO];
    [self.tableView registerNib:[UINib nibWithNibName:@"ImageReportArrayCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:IMAGEREPORT_ARRAY_CELL];
    [self setupFootView];
}

- (void)initData{
    
}

- (void)setupFootView{
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 88)];
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(30, 44, footer.frame.size.width - 60, 44)];
    btn.layer.cornerRadius = 22;
    [btn setBackgroundColor:Bluecolor];
    [btn setTitle:@"+ 添加类型" forState:UIControlStateNormal];
    [btn setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(addArrayItem) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:btn];
    self.tableView.tableFooterView = footer;
}

- (void)addArrayItem{
    [self.report_arr addObject:[RPImageChecknote new]];
    [self.tableView reloadData];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger n = self.report_arr.count;
    return n;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 265;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 16;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 16)];
    header.backgroundColor = COLOR(248, 248, 248);
    return header;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ImageReportArrayCell *cell = [tableView dequeueReusableCellWithIdentifier:IMAGEREPORT_ARRAY_CELL forIndexPath:indexPath];
    cell.delegate = self;
    [cell setupImagePicker:self];
    [cell refreshIndex:indexPath];
    
    RPImageChecknote *note = (RPImageChecknote*)self.report_arr[indexPath.section];
    cell.title_textfield.text = note.title;
    if (note.images && note.images.count>0) {
        [cell fillImages:note.images];
    }
    else if(note.imageurls){
        [cell fillImagesForUrl:note.imageurls];
    }
    
    [cell setDesc:note.desc];
    
    return cell;
}

#pragma mark - ImageReportArrayDelegate
- (void)sendTitle:(NSString *)title forIndexPath:(NSIndexPath*)indexpath{
    RPImageChecknote *note = self.report_arr[indexpath.section];
    note.title = title;
}

- (void)sendImgArr:(NSArray *)img_arr forIndexPath:(NSIndexPath *)indexpath{
    RPImageChecknote *note = self.report_arr[indexpath.section];
    note.images = [img_arr mutableCopy];
}

- (void)sendDesc:(NSString *)desc forIndexPath:(NSIndexPath*)indexpath{
    RPImageChecknote *note = self.report_arr[indexpath.section];
    note.desc = desc;
}

#pragma mark - 数据配置
- (NSMutableArray *)report_arr{
    if (!_report_arr) {
        _report_arr = [NSMutableArray arrayWithObject:[[RPImageChecknote alloc]init]];
    }
    return _report_arr;
}

@end
